package com.example.xl.springboot01.users.controller;

import com.example.xl.springboot01.users.bean.JdUser;
import com.example.xl.springboot01.users.bean.User;
import com.example.xl.springboot01.users.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@Data
//@AllArgsConstructor
@Controller
public class LoginController {
    @Autowired
    private IUserService userService;
    @GetMapping("/toLogin")
    public String login(ModelMap map) {
        map.addAttribute("host", "http://www.baidu.com");

        return "login";
    }
    /**
     * @Author GongXl
     * @Description 登录成功
     * @Date 004 10:00
     * @Param [map]
     * @return java.lang.String
    **/
    @RequestMapping("/login")
    public String index(HttpServletRequest req) {
        Object userName = req.getParameter("userName");
        Object password = req.getParameter("password");
     /*   Object reqUserName = req.getParameter("username");
        req.getSession().setAttribute("user","zs");*/
//        return "redirect:index_test";
        JdUser jdUser = new JdUser();
        jdUser.setUserName(userName.toString());
        jdUser.setPassword(password.toString());
        JdUser jdUserTmep = userService.querJdUser(jdUser);
        if(null != jdUserTmep){
            req.getSession().setAttribute("jdUser",jdUserTmep);
            return "index";//首页
        }
        req.setAttribute("msg","用户账号密码不存在。");
        return "login";//登录页

    }
    /**
     * @Author GongXl
     * @Description 注销
     * @Date 004 11:53
     * @Param [req]
     * @return java.lang.String
    **/
    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest req) {
       req.getSession().invalidate();
       return "login";//登录页
    }
}
